﻿using IDAL;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class DoctorDAL : IDoctorDAL
    {
        MyDBContext db = new MyDBContext();

        /// <summary>
        /// 医生添加方法
        /// </summary>
        /// <param name="doctor"></param>
        /// <returns></returns>
        public int AddDoctorDAL(Doctor doctor)
        {
            using (MyDBContext db = new MyDBContext())
            {
             
                db.Doctor.Add(doctor);
                return db.SaveChanges();
            }

        }



        /// <summary>
        /// 医生显示表
        /// </summary>
        /// <param name="Doctorname"></param>
        /// <returns></returns>
        /// 
        public List<Doctor> GetDoctorsList(string Doctorname = "")
        {

            var linq = (from d in db.Doctor
                        join o in db.Outpatient on d.Outpatient_Id equals o.Outpatient_Id
                        join p in db.Professional on d.Professional_Id equals p.Professional_Id
                        select new Doctor
                        {
                            Doctor_Id = d.Doctor_Id,
                            Doctor_IDnumber = d.Doctor_IDnumber,
                            Doctor_number = d.Doctor_number,
                            Doctor_Name = d.Doctor_Name,
                            Doctor_Sex = d.Doctor_Sex,
                            Doctor_Age = d.Doctor_Age,
                            Doctor_State = d.Doctor_State,

                            Outpatient_Name = o.Outpatient_Name,

                            Professional_Name = p.Professional_Name,
                            Doctor_Iphone = d.Doctor_Iphone

                        }).ToList();
            if (!string.IsNullOrEmpty(Doctorname))
            {
                linq = linq.Where(e => e.Doctor_Name.Contains(Doctorname)).ToList();
            }
            return linq;


        }
        /// <summary>
        /// 获取医生id进行获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Doctor GetDoctorById(int id)
        {
            using (MyDBContext db = new MyDBContext())
            {
                return db.Doctor.Where(c => c.Doctor_Id==id).FirstOrDefault();
            }

        }
        /// <summary>
        /// 修改接口
        /// </summary>
        /// <param name="doctor"></param>
        /// <returns></returns>
        public int UpdateDoctorDAL(Doctor doctor)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Doctor.Where(e => e.Doctor_Id == doctor.Doctor_Id).FirstOrDefault();

                list.Doctor_Name = doctor.Doctor_Name;
                list.Doctor_Age = doctor.Doctor_Age;
                list.Doctor_Email = doctor.Doctor_Email;
                list.Doctor_Remark = doctor.Doctor_Remark;
                list.Outpatient_Id = doctor.Outpatient_Id;
                list.Professional_Id = doctor.Professional_Id;
                list.Doctor_Sex = doctor.Doctor_Sex;
                list.Doctor_State = doctor.Doctor_State;
                list.Doctor_Home = doctor.Doctor_Home;
                list.Doctor_IDnumber = doctor.Doctor_IDnumber;
                list.Doctor_Birthday = doctor.Doctor_Birthday;
                list.Doctor_Iphone = doctor.Doctor_Iphone;

                return db.SaveChanges();
            }
        }

       
    }
}
